package com.jbinfo.mongoot.controller;

import com.jbinfo.mongoot.operation.plan.ServicePlan;
import com.jbinfo.mongoot.operation.plan.ServicePlanRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务计划
 * Created by wizard on 2017/4/5.
 */
@RestController
@RequestMapping("/service/plan")
@Api("fuzhou-servicePlan类API")
public class ServicePlanController {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ServicePlanRepository servicePlanRepository;

    @ApiOperation("通过post请求添加ServicePlan信息")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Map<String, Object> add(@ModelAttribute ServicePlan servicePlan) {
        Map<String, Object> map = new HashMap<String, Object>();
        String description="";
        //验证参数是否为空
        if(servicePlan.getService_project()==null ||  "".equals(servicePlan.getService_project())){
            description+= "服务项目不能为空";
        }
        if("".equals(description)){//非空
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            servicePlan.setCreate_date(simpleDateFormat.format(new Date()));
            servicePlan.setInvalid("1");
            servicePlanRepository.save(servicePlan);
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        map.put("description", description);
        map.put("code", "");
        return map;
    }

    /**
     * 根据oldid获取ServicePlanList
     * @param
     * @return
     */
    @ApiOperation("根据oldid获取老人服务计划集合")
    @RequestMapping(value = "/getListByOldid", method = RequestMethod.GET)
    public Map<String, Object>  findServicePlanByOldid(@RequestParam("oldid") String oldid ) throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        String description="";
        //验证参数是否为空
        if("".equals(description)){//非空
            Query query = new Query();
            Criteria criatira = new Criteria();
            criatira.andOperator(Criteria.where("oldid").is(oldid),Criteria.where("invalid").is("1"));
            List<ServicePlan> servicePlanList = mongoTemplate.find(query.addCriteria(criatira), ServicePlan.class);
            map.put("result", servicePlanList);
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        map.put("description", description);
        map.put("code", "");
        return map;
    }

    @ApiOperation("更新服务计划终止状态")
    @RequestMapping(value = "/removeServicePlanByOldid", method = RequestMethod.GET)
    public Map<String, Object> removeServicePlanById(@RequestParam("oldid") String oldid) {
        Map<String, Object> map = new HashMap<String, Object>();
        String description="";
        //验证参数是否为空
        if("".equals(description)){//非空
            mongoTemplate.updateMulti(new Query(Criteria.where("oldid").is(oldid)), new Update().set("invalid", "0"),ServicePlan.class);
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        map.put("description", description);
        map.put("code", "");
        return map;
    }


}
